import os

from fastapi import FastAPI, UploadFile

app = FastAPI()

UPLOAD_DIR = '/tmp'

DEFAULT_TBPU = "single_para"

@app.get("/")
async def root():
    return {"message": "Hello World"}

class FileParser:

    extensions = []

    def __init__(self):
        return

    async def parse_upload_file_to_text(self, file: UploadFile):
        local_file = os.path.join(UPLOAD_DIR, file.filename)
        with open(local_file, "wb") as f:
            content = await file.read()
            f.write(content)
        resp = self.parse_file_to_text(local_file)
        os.remove(local_file)
        return resp

    def parse_file_to_text(self, filename):
        return ""

    def can_parse(self, filename):
        valid = False
        for valid_type in self.extensions:
            if filename.lower().endswith(valid_type):
                valid = True
                break
        return valid

def valid_file_format(filename: str, extensions: list) -> bool:
    valid = False
    for valid_type in extensions:
        if filename.lower().endswith(valid_type):
            valid = True
            break
    return valid

def error(code, message):
    return send_resp(code, {}, message)

def success(data):
    return send_resp(200, data, "success")

def send_resp(code, data, message):
    return {"code": code, "data": data, "message": message}